// radarPages/radarOne/radarOne.js
import {
  get_open_id,
  get_radar_list
} from "../../utils/api.js";

const app = getApp()
var page = 1;
var selectedNews = false;
var newsList = [];
const util = require('../../utils/util')
const {
  formatTimeC
} = util

Page({

  /**
   * 页面的初始数据
   */
  data: {
    behaviorCount:{},
    radarInfo: [],
    dtype:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      dtype: options.dtype
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    page = 1;
    selectedNews = false;
    newsList = [];
    const that = this
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
      that.getRadarList()
    } else {
      wx.login({
        success(res) {
          let params = {
            code: res.code
          }
          app.HttpGetSend(get_open_id, params, function(result) {
            if (result.msg != "success") {
              app.showTipMsg(result.msg);
            } else {
              app.globalData.openid = result.data.openid.openid;
              wx.setStorage({
                key: 'openid',
                data: result.data.openid.openid
              })
              //如果注册过， 跳过注册
              app.globalData.userInfo = { ...result.data.userinfo }
              wx.setStorage({
                key: 'userInfo',
                data: JSON.stringify({ ...result.data.userinfo
                })
              })
              that.setData({
                userInfo: result.data.userinfo
              })
              // 获取雷达列表
              that.getRadarList()
            }
          })
        }
      })
    }
  },

  // 获取雷达列表
  getRadarList: function() {
    const that = this
    app.HttpGetSend(
      '/user_api/behaviorInfo', {
        uid: that.data.userInfo.id,
        type:that.data.dtype,
        page:page,
        page_size:20
      },
      function(res) {
        if (res.msg != "success") {
          app.showTipMsg(res.msg);
        } else {
          var radarInfo = res.data;
          if (radarInfo.length > 0) {
            const timestamp = parseInt(new Date().getTime() / 1000)
            for (let i = 0; i < radarInfo.length; i++) {
              radarInfo[i].uid = radarInfo[i].uid
              if (!!radarInfo[i].username) {
                radarInfo[i].username = radarInfo[i].username
              }else{
                radarInfo[i].username = '';
              }
              if (!!radarInfo[i].logo) {
                radarInfo[i].avatar = radarInfo[i].logo
              } else {
                radarInfo[i].avatar = '';
              }
              switch (radarInfo[i].type) {
                // 1.点赞 2.收藏 3.转发 4. 商城 5.查看 6.聊天窗口
                case 1:
                  radarInfo[i].type = radarInfo[i].type
                  // if (radarInfo[i].status == 0) {
                    radarInfo[i].action = '点赞了'
                    radarInfo[i].able = '你的'
                  radarInfo[i].content = '门店帮扶系统'
                    radarInfo[i].description = ',你的人脉圈很受欢迎'
                  // } else {
                  //   radarInfo[i].action = '取消点赞'
                  //   radarInfo[i].able = '你的'
                  //   radarInfo[i].content = '名片'
                  // }
                  break;

                case 2:
                  radarInfo[i].type = radarInfo[i].type
                  // if (radarInfo[i].status == 0) {
                    radarInfo[i].action = '收藏了'
                    radarInfo[i].able = '你的'
                  radarInfo[i].content = '门店帮扶系统'
                    radarInfo[i].description = ',你的人脉圈很受欢迎'
                  // } else {
                  //   radarInfo[i].action = '取消收藏'
                  //   radarInfo[i].able = '你的'
                  //   radarInfo[i].content = '名片'
                  // }
                  break;

                case 3:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '转发了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '门店帮扶系统'
                  radarInfo[i].description = ',你的人脉圈正在扩散'
                  break;

                case 4:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '进入了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商城'
                  break;

                case 5:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '查看了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '门店帮扶系统'
                  if (!!radarInfo[i].seeNum) {
                    radarInfo[i].description = '第' + radarInfo[i].seeNum + '次，强意向客户呦'
                  }else{
                    radarInfo[i].description = '第' + '' + '次，强意向客户呦'
                  }

                  break;

                case 6:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '进入聊天窗口'

                  break;

                case 7:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '访问了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '官网'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 8:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '查看了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '动态'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 9:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '播放了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '视频'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 10:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '播放了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '语音'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 11:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '访问了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商城'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 12:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '查看了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '广场分类'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 13:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '浏览了'
                  radarInfo[i].able = '你的名为'
                  if (radarInfo[i].goodsName.length>10){
                    var goodsName = '"' + radarInfo[i].goodsName.slice(0, 10) + '...' + '"'
                  } else if(radarInfo[i].goodsName.length == 0){
                    goodsName = ''
                  }else{
                    goodsName = '"' + radarInfo[i].goodsName + '"'
                  }
                  radarInfo[i].content = goodsName
                  radarInfo[i].able2 = '的商品'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 14:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '购买了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商品'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 15:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '点击付款了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '尽猎AI门店帮扶系统'

                  break;

                case 16:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '成功购买了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '尽猎AI门店帮扶系统'

                  break;

                case 17:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '添加了'
                  radarInfo[i].able = '新的'
                  radarInfo[i].content = '地址'

                  break;

                case 18:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '提交了'
                  radarInfo[i].able = '新的'
                  radarInfo[i].content = '商品订单'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 19:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '购买了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商品'
                  
                  break;

                case 20:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '取消了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商品订单'

                  break;

                case 21:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '点赞了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '动态'

                  break;

                default:
                  break;
              }
              let aim_timestamp = parseInt(new Date(radarInfo[i].add_time * 1000).getTime() / 1000)
              radarInfo[i].add_time = formatTimeC(timestamp, aim_timestamp)

              newsList.push(radarInfo[i]);
            }

          }

          if (radarInfo.length < 20) {
            selectedNews = false;
          } else {
            selectedNews = true;
            page += 1;
          }

          that.setData({
            radarInfo: newsList
          })
          
          wx.hideLoading();
        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (selectedNews == true) {
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getRadarList();

    } else {
      wx.hideLoading();
    }
  }
})